package com.paic.custserv.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.paic.custserv.domain.CussFaqCategder;
import com.paic.custserv.mapper.CussFaqCategderMapper;
import com.paic.custserv.service.ICussFaqCategderService;

@Service("cussFaqCategderService")
public class CussFaqCategderServiceImpl implements ICussFaqCategderService {
	
	@Autowired
	private CussFaqCategderMapper cussFaqCategderMapper;

	@Override
	public void save(CussFaqCategder model) {
		cussFaqCategderMapper.insertSelective(model);
	}

	@Override
	public void update(CussFaqCategder model) {
		cussFaqCategderMapper.updateByPrimaryKeySelective(model);
	}

	@Override
	public List<CussFaqCategder> queryList(CussFaqCategder model) {
		return cussFaqCategderMapper.queryList(model);
	}

	/**
	 * 查询常见问题分类 初始化树
	 */
	public List<Map<String,Object>> getCussFaqCategory(Integer parentId) throws Exception{
		
		List<Map<String,Object>> resList = new ArrayList<Map<String,Object>>();
		CussFaqCategder cussFaqCategder = new CussFaqCategder();
		if(parentId == null){//设置root节点
			cussFaqCategder.setIdFaqCategory(-1);
			List<CussFaqCategder> list = this.queryList(cussFaqCategder);
			Map<String,Object> tempMap = new HashMap<String,Object>();
			tempMap.put("categoryName", list.get(0).getCategoryName());
			tempMap.put("depth", list.get(0).getDepth());
			tempMap.put("isParent","true");
			tempMap.put("idFaqCategory", list.get(0).getIdFaqCategory());
			resList.add(tempMap);
		}else{
			List<Integer> depths = null;
			if(parentId == -1){
				depths = new ArrayList<Integer>();
				depths.add(1);
			}
			cussFaqCategder.setDepths(depths);
			cussFaqCategder.setParentCategoryId(parentId);
			List<CussFaqCategder> list = this.queryList(cussFaqCategder);
			//构建一级或二级类目
			for(int i = 0;i < (list == null ? 0 : list.size());i++){
				Map<String,Object> tempMap = new HashMap<String,Object>();
				tempMap.put("categoryName", list.get(i).getCategoryName());
				tempMap.put("depth", list.get(i).getDepth());
				tempMap.put("isParent","true");
				tempMap.put("levels",list.get(i).getLevels());
				tempMap.put("idFaqCategory", list.get(i).getIdFaqCategory());
				resList.add(tempMap);
			}
		}
		/*
		//构建二级类目
		for(int i = 0;i < (firstLevels == null || list == null ? 0 : firstLevels.size());i++){
			List<Map<String,Object>> vals = new ArrayList<Map<String,Object>>();
			Map<Object, Object> resMap = new HashMap<Object,Object>(); 
			for(CussFaqCategder faqCategory : list){
				if(faqCategory.getParentCategoryId() == firstLevels.get(i).get("idFaqCategory") 
						&& faqCategory.getDepth() != 1){
					Map<String,Object> tempMap = new HashMap<String,Object>();
					tempMap.put("categoryName", faqCategory.getCategoryName());
//					tempMap.put("parentCategoryId", faqCategory.getParentCategoryId());
					tempMap.put("idFaqCategory", faqCategory.getIdFaqCategory());
					vals.add(tempMap);
				}
			}
			resMap.put(firstLevels.get(i).get("categoryName"), vals);
			resList.add(resMap);
		}
		*/
		return resList;
	}


}
